package com.cuit.filter;


import com.cuit.constants.Constants;
import com.cuit.enums.RpcConfigEnum;
import com.cuit.extension.Activate;
import com.cuit.factory.SingleFactory;
import com.cuit.remoting.dto.RpcResponse;
import com.cuit.timeout.CompletableFutureTimeout;
import com.cuit.utils.PropertiesFileUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
@Activate(group = Constants.CONSUMER, order = 50)
public class TimeOutFilter implements Filter{

    @Override
    public void doFilter(FilterContext fIlterContext) throws Exception {
        Long timeout = fIlterContext.getRpcRequest().getTimeout();
        fIlterContext.getFilterChain().doFilter(fIlterContext);

        if (timeout > 0){
            CompletableFuture<RpcResponse<Object>> resultFuture = fIlterContext.getResultFuture();
            resultFuture = CompletableFutureTimeout.within(resultFuture, timeout, TimeUnit.MILLISECONDS);
            fIlterContext.setResultFuture(resultFuture);
        }

    }
}
